-
Notifications
You must be signed in to change notification settings - Fork 23
Align have_relationship matcher with readme and add support for Symbol or String parameters #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
1fd1c91
to
2859c6f
Compare
Added enabled: true for all new cops to remove warnings about then when running rubocop. Added ignore for block length to specs directory as many specs blocks are longer than the maximum.
Create .rspec file to remove the need to require spec_helper in all specs.
…arameters). The current README file provides an example usage for the matcher of * `expect(document['data']).to have_relationships(:posts, :comments)` With the current implementation of have_relationships, this fails as it does not to_s the relationship array values provided. Minor fix to correct this.
Add support for symbol keyed hashes to the have_jsonapi_object matcher and create related spec tests.
Add support for symbol keyed hashes to the have_meta matcher and create related spec tests.
Add support for symbol keyed hashes to the have_relationship matcher and create related spec tests.
2859c6f
to
8a0720e
Compare
Add support for using a symbol as the type and create related spec tests.
For consistency, as all other specs now contain tests for symbols and strings, added string tests into the attributes and links specs.
8a0720e
to
265d2ed
Compare
Superseded by #18 |
@stas - just looking at what eventually went into master - the aim of this PR was to allow all the |
@sa73917 I'm taking a second look and I don't think I follow. Mainly, it does look like I'll be happy to merge the fix if you can open another PR that does not allow symbols and strings to match attributes and relationships in the same time though. Sorry for missing out on the initial issue that was raised. 🙈 |
Sounds like your change to the code in my PR was intentional then (which is unfortunate IMO). Whilst I understand a true JSON document can't have symbols in it, at the end of the day this is a Ruby gem that helps test JSON documents so (at least from my point of view) it should allow ruby developers to use it much like they would expect to interact with the vast majority of ruby gems (and in fact most standard ruby methods and objects) Clearly personal opinion but I'd much prefer to read: expect(document[:data]).to have_type(:users) rather than: expect(document['data']).to have_type('users') My expectation when passing symbols into the matcher is (obviously) not that the type attribute will have a symbol as its value (as that would be impossible). I'm just expecting the matcher will include a "to_s" on their expected type. Eg. module JSONAPI
module RSpec
module Type
::RSpec::Matchers.define :have_type do |expected|
match do |actual|
JSONAPI::RSpec.as_indifferent_hash(actual)['type'] == expected.to_s
end
end
end
end
end Oh well, if you change your mind and would like a PR that ensures all the matchers will accept symbols, I'm more than happy to do so. I'll respectfully decline the reverse however, as I'm loath to remove the symbol parameter functionality from the matchers that already accept them - I'd have to change a bunch of my specs into a format I much less prefer to read. :-) |
I think we should make it clear that without And only if you set up I think the default behaviour is valid only for folks who really want to encourage a specific way of formatting the tests/jsonapi-documents, but for the most of the users, Let me know if that makes sense. And I would definitely not mind updating those two matchers that force the string-based matching. Thank you for clarifying this @sa73917! |
No worries - always hard to understand intent in text isn't it! :-) If we are to go down the path of the the Is that what you're thinking? |
Nope. What I'm trying to say is that... If you're trying to use the matchers on a document like this with strings: {
data: {
attributes: {
id: "1",
email: "my@domain.tld"
}
}
} it should fail, and succeed on symbols (since obviously the document uses symbols (likewise with the opposite: string keys, and trying to match with symbol names). If you have enabled the Basically, I don't want us to alter the document/matcher arguments, unless we are explicitly told |
I think we're in violent agreement on the true setting with On the false setting though - my thought was assume the source must be a valid JSON document and hence parameters must be strings. Sounds like you'd like to leave source and parameters alone and just compare directly. So if it's a symbolized source document, it should only match symbolized parameters. If it's a string source document it should only match string parameters. Do I have it now? :) |
I'm afraid this can still be confusing since we'd have to convert to strings for |
Rather than go back and forth trying to describe this - here's a branch with my suggested change. At a high level, it basically means each matcher calls (I did think it might be worth having a |
@sa73917 I went ahead and made it clear that we will always convert matcher arguments where JSON:API spec dictates the format. I'm considering this conversation resolved for now. Thank you for helping with it! |
What is the current behavior?
The current README file provides an example usage for the matcher of
expect(document['data']).to have_relationships(:posts, :comments)
With the current implementation of have_relationships, this fails as it does not to_s the relationship array values provided.
PR #15 was rejected as it only added Symbol parameters to the relationships matcher without updating all matchers.
This PR includes support for symbol parameters in all matchers
What is the new behavior?
All matchers now accept strings or symbols (and string or symbol keyed hashes where applicable)
Relationships matcher now has a spec to test it.
Checklist
Please make sure the following requirements are complete:
features)